package com.soo.hoo.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soo.hoo.admin.mapper.SysUserFamilyMapper;
import com.soo.hoo.admin.model.entity.SysUserFamily;
import com.soo.hoo.admin.model.vo.UserVO;
import com.soo.hoo.admin.service.UserFamilyService;
import com.soo.hoo.admin.utils.SaLoginUtil;
import com.soo.hoo.core.exception.GlobalException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户所属家庭，一个用户只会有一个家庭 服务实现类
 * </p>
 *
 * @author 杨燚
 * @since 2024-04-20
 */
@Service
public class UserFamilyServiceImpl extends ServiceImpl<SysUserFamilyMapper, SysUserFamily> implements UserFamilyService {


    @Override
    public String generateFamilyId() {
        return IdUtil.nanoId();
    }


    @Override
    public void setFamily(Long userId, String mainFamilyId, String... familyIds) {
        UserVO user = SaLoginUtil.getUser();
        if (user == null) {
            throw new GlobalException("获取用户信息发生异常");
        }
        Wrapper<SysUserFamily> wrapper = new LambdaQueryWrapper<SysUserFamily>()
                .eq(SysUserFamily::getUserId, userId)
                .eq(SysUserFamily::getFamilyId, mainFamilyId);
        SysUserFamily uf = baseMapper.selectOne(wrapper);
        if (uf == null) {
            uf = new SysUserFamily();
            uf.setUserId(userId);
            uf.setUserName(SaLoginUtil.getUser().getName());
            uf.setFamilyId(mainFamilyId);
            if (familyIds != null && familyIds.length > 0) {
                uf.setFamilyIds(String.join(",", familyIds));
            }
            baseMapper.insert(uf);
            return;
        }
        if (familyIds != null && familyIds.length > 0) {
            if (StrUtil.isNotBlank(uf.getFamilyIds())) {
                uf.setFamilyIds(uf.getFamilyIds() + "," + String.join(",", familyIds));
                return;
            }
            uf.setFamilyIds(String.join(",", familyIds));
            baseMapper.updateById(uf);
        }
    }

    @Override
    public SysUserFamily getFamily(Long userId) {
        LambdaQueryWrapper<SysUserFamily> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserFamily::getUserId, userId);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public SysUserFamily getFamilyByFamilyId(String familyId) {
        LambdaQueryWrapper<SysUserFamily> wp =
                new LambdaQueryWrapper<SysUserFamily>()
                        .eq(SysUserFamily::getFamilyId, familyId);
        return baseMapper.selectOne(wp);
    }

    @Override
    public List<SysUserFamily> getFamilyByFamilyId(List<String> familyIds) {
        LambdaQueryWrapper<SysUserFamily> wp =
                new LambdaQueryWrapper<SysUserFamily>()
                        .in(SysUserFamily::getFamilyId, familyIds);
        return baseMapper.selectList(wp);
    }

    @Override
    public String getUserFamilyId(Long userId) {
        SysUserFamily family = getFamily(userId);
        if (family == null) {
            return this.generateFamilyId();
        }
        return family.getFamilyId();
    }
}
